package com.twb.core.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * @author DHL
 * @ClassName: BigNumber
 * @Description: 精确计算
 * @date 2022/8/18、16:16
 */
public class BigNumber {
    private BigDecimal bigNum;

    public BigNumber() {
        this.bigNum = BigDecimal.ZERO;
    }

    public BigNumber(BigDecimal bigNum) {
        if (bigNum == null) {
            bigNum = BigDecimal.ZERO;
        }
        this.bigNum = bigNum;
    }


    /**
     * @return java.math.BigDecimal
     * @Title parse
     * @Description 解析值
     * @author DHL
     * @date 16:42 2022/8/18
     */
    public BigDecimal parse() {
        return bigNum;
    }


    /////////////////// 加法 ///////////////////

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title add
     * @Description 加法运算
     * @author DHL
     * @date 16:22 2022/8/18
     */
    public BigNumber add(BigDecimal num) {
        this.bigNum = bigNum.add(num);
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title add
     * @Description 加法运算
     * @author DHL
     * @date 16:23 2022/8/18
     */
    public BigNumber add(String num) {
        if (StringUtils.isBlank(num)) {
            num = "0";
        }

        this.bigNum = bigNum.add(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title add
     * @Description 加法运算
     * @author DHL
     * @date 16:26 2022/8/18
     */
    public BigNumber add(Long num) {
        if (num == null) {
            num = 0L;
        }

        this.bigNum = bigNum.add(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title add
     * @Description 加法运算
     * @author DHL
     * @date 16:27 2022/8/18
     */
    public BigNumber add(Double num) {
        if (num == null) {
            num = 0.0;
        }

        this.bigNum = bigNum.add(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title add
     * @Description 加法运算
     * @author DHL
     * @date 16:29 2022/8/18
     */
    public BigNumber add(Float num) {
        if (num == null) {
            num = 0f;
        }

        this.bigNum = bigNum.add(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title add
     * @Description 加法运算
     * @author DHL
     * @date 16:30 2022/8/18
     */
    public BigNumber add(Integer num) {
        if (num == null) {
            num = 0;
        }

        this.bigNum = bigNum.add(new BigDecimal(num));
        return this;
    }


    /////////////////// 减法 ///////////////////

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title subtract
     * @Description 减法运算
     * @author DHL
     * @date 16:22 2022/8/18
     */
    public BigNumber subtract(BigDecimal num) {
        this.bigNum = bigNum.subtract(num);
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title subtract
     * @Description 减法运算
     * @author DHL
     * @date 16:23 2022/8/18
     */
    public BigNumber subtract(String num) {
        if (StringUtils.isBlank(num)) {
            num = "0";
        }

        this.bigNum = bigNum.subtract(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title subtract
     * @Description 减法运算
     * @author DHL
     * @date 16:26 2022/8/18
     */
    public BigNumber subtract(Long num) {
        if (num == null) {
            num = 0L;
        }

        this.bigNum = bigNum.subtract(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title subtract
     * @Description 减法运算
     * @author DHL
     * @date 16:27 2022/8/18
     */
    public BigNumber subtract(Double num) {
        if (num == null) {
            num = 0.0;
        }

        this.bigNum = bigNum.subtract(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title subtract
     * @Description 减法运算
     * @author DHL
     * @date 16:29 2022/8/18
     */
    public BigNumber subtract(Float num) {
        if (num == null) {
            num = 0f;
        }

        this.bigNum = bigNum.subtract(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title subtract
     * @Description 减法运算
     * @author DHL
     * @date 16:30 2022/8/18
     */
    public BigNumber subtract(Integer num) {
        if (num == null) {
            num = 0;
        }

        this.bigNum = bigNum.subtract(new BigDecimal(num));
        return this;
    }


    /////////////////// 乘法 ///////////////////

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title multiply
     * @Description 乘法运算
     * @author DHL
     * @date 16:22 2022/8/18
     */
    public BigNumber multiply(BigDecimal num) {
        this.bigNum = bigNum.multiply(num);
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title multiply
     * @Description 乘法运算
     * @author DHL
     * @date 16:23 2022/8/18
     */
    public BigNumber multiply(String num) {
        if (StringUtils.isBlank(num)) {
            num = "0";
        }

        this.bigNum = bigNum.multiply(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title multiply
     * @Description 乘法运算
     * @author DHL
     * @date 16:26 2022/8/18
     */
    public BigNumber multiply(Long num) {
        if (num == null) {
            num = 0L;
        }

        this.bigNum = bigNum.multiply(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title multiply
     * @Description 乘法运算
     * @author DHL
     * @date 16:27 2022/8/18
     */
    public BigNumber multiply(Double num) {
        if (num == null) {
            num = 0.0;
        }

        this.bigNum = bigNum.multiply(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title multiply
     * @Description 乘法运算
     * @author DHL
     * @date 16:29 2022/8/18
     */
    public BigNumber multiply(Float num) {
        if (num == null) {
            num = 0f;
        }

        this.bigNum = bigNum.multiply(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title multiply
     * @Description 乘法运算
     * @author DHL
     * @date 16:30 2022/8/18
     */
    public BigNumber multiply(Integer num) {
        if (num == null) {
            num = 0;
        }

        this.bigNum = bigNum.multiply(new BigDecimal(num));
        return this;
    }


    /////////////////// 除法 ///////////////////

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title divide
     * @Description 除法运算
     * @author DHL
     * @date 16:22 2022/8/18
     */
    public BigNumber divide(BigDecimal num) {
        this.bigNum = bigNum.divide(num);
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title divide
     * @Description 除法运算
     * @author DHL
     * @date 16:23 2022/8/18
     */
    public BigNumber divide(String num) {
        if (StringUtils.isBlank(num)) {
            num = "0";
        }

        this.bigNum = bigNum.divide(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title divide
     * @Description 除法运算
     * @author DHL
     * @date 16:26 2022/8/18
     */
    public BigNumber divide(Long num) {
        if (num == null) {
            num = 0L;
        }

        this.bigNum = bigNum.divide(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title divide
     * @Description 除法运算
     * @author DHL
     * @date 16:27 2022/8/18
     */
    public BigNumber divide(Double num) {
        if (num == null) {
            num = 0.0;
        }

        this.bigNum = bigNum.divide(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title divide
     * @Description 除法运算
     * @author DHL
     * @date 16:29 2022/8/18
     */
    public BigNumber divide(Float num) {
        if (num == null) {
            num = 0f;
        }

        this.bigNum = bigNum.divide(new BigDecimal(num));
        return this;
    }

    /**
     * @param num
     * @return com.twb.core.util.BigNumber
     * @Title divide
     * @Description 除法运算
     * @author DHL
     * @date 16:30 2022/8/18
     */
    public BigNumber divide(Integer num) {
        if (num == null) {
            num = 0;
        }

        this.bigNum = bigNum.divide(new BigDecimal(num));
        return this;
    }


    /////////////////// 保留小数，四舍五入 ///////////////////

    /**
     * @param scale
     * @return com.twb.core.util.BigNumber
     * @Title round
     * @Description 保留小数，四舍五入
     * @author DHL
     * @date 16:40 2022/8/18
     */
    public BigNumber round(int scale) {
        if (scale < 0) {
            scale = 0;
        }

        this.bigNum = bigNum.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return this;
    }
}
